package de.slothsoft.wasbinich.android;

import java.text.MessageFormat;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.plus.PlusShare;

import de.slothsoft.wasbinich.android.util.StyleUtil;
import de.slothsoft.wasbinich.core.GameResult;
import de.slothsoft.wasbinich.core.WasBinIch;
import de.slothsoft.wasbinich.core.util.GameResultUtil;

public class ResultActivity extends AbstractActivity implements OnClickListener {

    public static final String ARG_RESULT = "result"; //$NON-NLS-1$

    private GameResult result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_result);
	hookButtonListener();

	this.result = (GameResult) getIntent().getSerializableExtra(ARG_RESULT);
	updateFieldsFromResult();

	findViewById(R.id.result_gplus).setVisibility(isGooglePlusInstalled() ? View.VISIBLE : View.INVISIBLE);
    }

    public boolean isGooglePlusInstalled() {
	try {
	    getPackageManager().getApplicationInfo("com.google.android.apps.plus", 0); //$NON-NLS-1$
	    return true;
	} catch (PackageManager.NameNotFoundException e) {
	    return false;
	}
    }

    private void updateFieldsFromResult() {
	((TextView) findViewById(R.id.result_field_correct)).setText("" + this.result.correctAnswers); //$NON-NLS-1$
	((TextView) findViewById(R.id.result_field_sum)).setText("" + this.result.entireAnswers); //$NON-NLS-1$
	((TextView) findViewById(R.id.result_field_message)).setText(GameResultUtil
		.buildPercentAndRatingString(this.result));
    }

    private void hookButtonListener() {
	hookFacebookButtonListener(R.id.result_facebook);
	hookGplusButtonListener(R.id.result_gplus);
	hookButtonListener(R.id.result_continue);
	hookButtonListener(R.id.result_exit);
    }

    private void hookFacebookButtonListener(int id) {
	Button button = (Button) findViewById(id);
	button.setOnClickListener(this);
	StyleUtil.styleFacebookButton(this, button);
    }

    private void hookGplusButtonListener(int id) {
	Button button = (Button) findViewById(id);
	button.setOnClickListener(this);
	StyleUtil.styleGplusButton(this, button);
    }

    private void hookButtonListener(int id) {
	Button button = (Button) findViewById(id);
	button.setOnClickListener(this);
	StyleUtil.styleButton(this, button);
    }

    @Override
    public void onClick(View view) {
	int id = view.getId();
	if (id == R.id.result_gplus) {
	    shareOnGooglePlus();
	} else if (id == R.id.result_facebook) {
	    shareOnFacebook();
	} else if (id == R.id.result_continue) {
	    continuePlaying();
	} else if (id == R.id.result_exit) {
	    exit();
	}
    }

    private void shareOnGooglePlus() {
	Intent shareIntent = new PlusShare.Builder(this).setType("text/plain").setText(getSocialMessage()).getIntent(); //$NON-NLS-1$
	startActivityForResult(shareIntent, 0);
    }

    private void shareOnFacebook() {
	Intent shareIntent = new Intent(this, FacebookActivity.class);
	shareIntent.putExtra(FacebookActivity.ARG_MESSAGE, getSocialMessage());
	startActivityForResult(shareIntent, 0);
    }

    private String getSocialMessage() {
	return MessageFormat.format(getString(R.string.result_social_message), this.result.correctAnswers,
		this.result.entireAnswers, GameResultUtil.buildRatingString(this.result), WasBinIch.APPLICATION_URL);
    }

    private void continuePlaying() {
	startActivity(new Intent(this, QuestionActivity.class));
    }

    private void exit() {
	onBackPressed();
    }

    @Override
    public void onBackPressed() {
	NavUtils.navigateUpFromSameTask(this);
    }

}
